package com.pmsystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Administrator;
import com.pmsystem.pojo.Opinion;
import com.pmsystem.pojo.User;
import com.pmsystem.service.OpinionService;
import com.pmsystem.util.BeanUtil;

@Controller
public class OpinionController {
	@Autowired
	private OpinionService opinionService;
	
	/**
	 * 
	 * <p>Title: solveOpinion</p >  
	 * <p>Description: 处理反馈</p >  
	 * @author 泽强
	 * @param session
	 * @param opinion
	 * @return
	 */
	@RequestMapping(value = "admin/solveOpinion", method = RequestMethod.POST)
	@ResponseBody
	public String  solveOpinion(HttpSession session,@RequestParam Map<String,Object> map) {
		Opinion opinion = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Opinion.class);
		Administrator admin = (Administrator) session.getAttribute("admin");
		opinionService.solveOpinion(admin,opinion);
		return "success";
	}
	
	/**
	 * 	
	 * <p>Title: findAllOpinion</p >  
	 * <p>Description: 分页查询反馈</p >  
	 * @author 泽强
	 * @param pageNo
	 * @param pageSize
	 */
	@RequestMapping(value = "admin/findAllOpinion", method = RequestMethod.GET)
	public Object findAllOpinion(Integer pageNo,Integer pageSize) {
		List<Opinion> list = opinionService.findAllOpinion(pageNo,pageSize);
		return list;
	}
	
	/**
	 * 
	 * <p>Title: findOpinionByAdminId</p >  
	 * <p>Description: 查找反馈</p >  
	 * @author 泽强
	 * @param session
	 */
	@RequestMapping(value = "admin/findOpinionByAdminId", method = RequestMethod.GET)
	public Object findOpinionByAdminId(HttpSession session) {
		Administrator admin = (Administrator)session.getAttribute("admin");
		Integer adminId = admin.getAdminId();
		List<Opinion> list = opinionService.findByAdminId(adminId);
		return list;
	}
	
	/**
	 * 
	 * <p>Title: userProposeOpinion</p >  
	 * <p>Description: 业主提出反馈</p >  
	 * @author 泽强
	 * @param opinion
	 * @return
	 */
	@RequestMapping(value="user/proposeOpinion",method=RequestMethod.POST )
	@ResponseBody
	public Map<String, Object> userProposeOpinion(HttpSession session,@RequestParam Map<String,Object> map) {
		User user = (User) session.getAttribute("user");
		int userId = user.getUserId();	
		Opinion opinion = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Opinion.class);
		opinion.setUserId(userId);
		boolean flag=opinionService.addOpinion(opinion);
		map = new HashMap<>();
		if(flag) {
			map.put("msg", "success") ;
		}else {
			map.put("msg", "error") ;
		}
		return map;
	}
	
	/**
	 * 
	 * <p>Title: adminProposeOpinion</p >  
	 * <p>Description: 管理员提出反馈</p >  
	 * @author 泽强
	 * @param session
	 * @param map
	 * @return
	 */
	@RequestMapping(value="admin/proposeOpinion",method=RequestMethod.POST )
	@ResponseBody
	public Map<String, Object> adminProposeOpinion(HttpSession session,@RequestParam Map<String,Object> map) {
		Administrator admin = (Administrator)session.getAttribute("admin");
		Integer adminId = admin.getAdminId();
		Opinion opinion = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Opinion.class);
		opinion.setAdminstratorId(adminId);
		boolean flag=opinionService.addOpinion(opinion);
		map = new HashMap<>();
		if(flag) {
			map.put("msg", "success") ;
		}else {
			map.put("msg", "error") ;
		}
		return map;
	}
	
	/**
	 * 
	 * <p>Title: findOwnOpinion</p >  
	 * <p>Description: 用户查找自己的反馈</p >  
	 * @author 泽强
	 * @param session
	 */
	@RequestMapping(value="user/findOwnOpinion",method=RequestMethod.GET)
	@ResponseBody
	public Object findOwnOpinion(HttpSession session) {
		User user = (User) session.getAttribute("user");
		int userId = user.getUserId();
		List<Opinion> list =opinionService.findByUserId(userId);
		return list;
	}
	
	/**
	 * 
	 * <p>Title: findOpinion</p >  
	 * <p>Description: 按一定条件查询反馈，注意SQL语句</p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@RequestMapping(value="findOpinion",method=RequestMethod.GET)
	@ResponseBody
	public Object findOpinion(@RequestParam Map<String , Object > map) {
		List<Opinion> list =opinionService.findOpinion(map);
		return list;
	}
	
}
